package com.base.timer
{
	import com.base.data.DataCenter;
	import com.base.data.sendrule.SendRuleBean;
	import com.base.event.CustomEvent;
	import com.base.util.Utils;
	
	import flash.desktop.NativeApplication;
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.events.TimerEvent;
	import flash.utils.Timer;
	
	import mx.collections.ArrayCollection;

	public class TimerManager extends EventDispatcher
	{
		private var timer:Timer;
		
		private var delay:Number = 1000 * 60;
		
		private static var _instance : TimerManager;
		
		public static function get instance() : TimerManager {
			if ( !_instance ) {
				_instance = new TimerManager();
			}
			return _instance;
		}
		
		public function TimerManager()
		{
			if( _instance ) throw new Error("请使用单例访问");
		}
		
		public function init( fun:Function ):void {
			timer = new Timer( delay );
			timer.addEventListener( TimerEvent.TIMER , onTimer );
			timer.start();
			if( fun != null ) fun();
		}
		
		private function onTimer( e:Event ):void {
			var d:Date = new Date();
			for each( var rule:SendRuleBean in DataCenter.instance.ruleList ) {
				if( Utils.dateEquals( d , rule.d1 ) || Utils.dateEquals( d , rule.d2 )
				 || Utils.dateEquals( d , rule.d3 )  || Utils.dateEquals( d , rule.d4 )
				 || Utils.dateEquals( d , rule.d5 )  || Utils.dateEquals( d , rule.d6 )
				 || Utils.dateEquals( d , rule.d7 )  || Utils.dateEquals( d , rule.d8 )
				 || Utils.dateEquals( d , rule.d9 ) ) {
					NativeApplication.nativeApplication.dispatchEvent( new CustomEvent( CustomEvent.Send_Time , rule ) );
				}
			}
			
		}
		
	}
}